#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/socket.h>  // socklen_t
#include <network/dual_stack_socket.h>


int main(int argc, char** argv)
{
    char buf[1024] = {0};
    int slen = 0;

    char* addr = "::1";
    int port = 11024;

    if (argc >= 3)
    {
        addr = argv[1];
        port = atoi(argv[2]);
    }

    dualstack_socket_t s = create_dual_socket(addr, port, protocol_type_udp);

    if (!s)
    {
        printf("Create socket failed!\n");
        exit(1);
    }
    printf("Create socket succeed!\n");

    if (bind(get_socket_fd(s), get_sockaddr(s), get_sockaddr_len(s))==-1)
    {
        printf("bind failed!\n");
        exit(1);
    }
    printf("Bind succeed!\n");

    if (-1 == listen(get_socket_fd(s), 5)) {  
        printf("listen error\n");
        exit(1);  
    }
        printf("Bind succeed!\n");


    slen = get_sockaddr_len(s);
    while(1)
    {
        recvfrom(get_socket_fd(s), buf, sizeof(buf) - 1, 0, get_sockaddr(s), (socklen_t*)&slen);
        printf("%s\n", buf);
    }

    printf("Recv Ok!\n");

    destroy_dual_socket(s);

    return 0;
}

